package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_XwScenicAtlasinfo;
import com.xwdeshj.entity.XwFarmAtlasinfo;
import com.xwdeshj.entity.XwScenicAtlasinfo;
import com.xwdeshj.entity.XwScenicBasicinfo;
import com.xwdeshj.repository.XwScenicAtlasinfoRepository;
import com.xwdeshj.service.XwScenicAtlasinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by BKWJA on 2019/1/7.
 * 景区图集信息
 */
@Slf4j
@Service("xwScenicAtlasinfoService")
public class XwScenicAtlasinfoServiceImpl implements XwScenicAtlasinfoService {

    @Autowired
    private XwScenicAtlasinfoRepository xwScenicAtlasinfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_XwScenicAtlasinfo dal_xwScenicAtlasinfo = new Dal_XwScenicAtlasinfo();

    /**
     * 景区——新增图集
     * @param parmJson  scenic_encryptId:农家乐加密id，imglink：图片链接url
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject savescenicImgurl(String parmJson, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!parmJson.equals("")){
                JSONArray jsonArray = new JSONArray();
                jsonArray = JSON.parseArray(parmJson);
                for (int i=0 ; i < jsonArray.size() ; i++){
                    JSONObject object = jsonArray.getJSONObject(i);
                    String id = object.getString("id");
                    String business_encryptId = object.getString("business_encryptId");
                    String scenic_encryptId = object.getString("scenic_encryptId");
                    String imglink = object.getString("imglink");
                    if (imglink.equals("")){
                        //删除
                        XwScenicAtlasinfo xwScenicAtlasinfo = new XwScenicAtlasinfo();
                        xwScenicAtlasinfo.setBusinessEncryptid(business_encryptId);
                        xwScenicAtlasinfo.setScenicEncryptid(scenic_encryptId);
                        xwScenicAtlasinfo.setScenicAtlasId(Integer.parseInt(id));
                        boolean temp = xwScenicAtlasinfoRepository.exists(Example.of(xwScenicAtlasinfo));
                        if(temp){
                            xwScenicAtlasinfoRepository.deleteByScenicAtlasId(Integer.parseInt(id));
                        }
                        continue;
                    }else{
                        JSONArray array =  dal_xwScenicAtlasinfo.getScenicAtlasinfo(business_encryptId,scenic_encryptId,
                                id,jdbcTemplate);
                        if (array.size() == 0){
                            XwScenicAtlasinfo xwScenicAtlasinfo = new XwScenicAtlasinfo();
                            xwScenicAtlasinfo.setBusinessEncryptid(business_encryptId);
                            xwScenicAtlasinfo.setScenicEncryptid(scenic_encryptId);
                            xwScenicAtlasinfo.setScenicatlasImg(imglink);
                            xwScenicAtlasinfo.setScenicAtlasCreatetime(new Date());
                            xwScenicAtlasinfo.setScenicAtlasUpdatetime(new Date());
                            xwScenicAtlasinfoRepository.save(xwScenicAtlasinfo);

                        }else {
                            XwScenicAtlasinfo xwScenicAtlasinfo = new XwScenicAtlasinfo();
                            xwScenicAtlasinfo.setScenicAtlasId(Integer.parseInt(id));
                            boolean temp = xwScenicAtlasinfoRepository.exists(Example.of(xwScenicAtlasinfo));
                            if (temp){
                                XwScenicAtlasinfo scenicAtlasinfo = xwScenicAtlasinfoRepository.findByScenicAtlasId(Integer.parseInt(id));
                                scenicAtlasinfo.setScenicatlasImg(imglink);
                                scenicAtlasinfo.setScenicAtlasUpdatetime(new Date());
                                xwScenicAtlasinfoRepository.save(scenicAtlasinfo);
                            }
                        }
                    }
                }

                result.put("status",0);
                result.put("msg","ok");
            }else {
                result.put("status",40001);
                result.put("msg","uid不能为空,请先登录！");
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("savefarmImgurl:"+result.toString());
        return result;
    }

    /**
     *  景区——获取图集 弹框中的已有的图片
     */
    @Override
    public JSONObject getscenicaltlasinfowin(String scenic_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwScenicAtlasinfo.getscenicaltlasinfowin(scenic_encryptId,jdbcTemplate);

        System.out.println("getscenicaltlasinfowin:"+result.toString());
        return result;
    }



    public Dal_XwScenicAtlasinfo getDal_xwScenicAtlasinfo() {
        return dal_xwScenicAtlasinfo;
    }

    public void setDal_xwScenicAtlasinfo(Dal_XwScenicAtlasinfo dal_xwScenicAtlasinfo) {
        this.dal_xwScenicAtlasinfo = dal_xwScenicAtlasinfo;
    }
}
